***** Set directories 
local dir_clean 	"~/Dropbox/Retirement gaming/clean"
local dir_output 	"~/Dropbox/Retirement gaming/output/dataverse"

* Get retirement sample data and keep sample observed in private pension as empl or self_empl at 45-57
use "`dir_clean'/retirementsample.dta", clear

g marksage = agemonths_centered>=-60 & agemonths_centered<=95
g aux=aportaci==1 & (status_1==1 | status_3==1) & tipREM==1
cap drop indcom
bys i t: egen indcom=max(aux)
g indcomage=indcom*marksage
bys i: egen nrmonths_indcomage=sum(indcomage)  // counts total nr months reported in industria&comercio for each individual in the sample age (45 to 57)	
keep if nrmonths_indcomage>=6 // sample are those observed at least 6 months reporting to industria & comercio at ages 45 to 57	
	
g cohort_retir65= birth_month<tm(1950m4)

keep if merge==3 & age_benefits>=40
keep if markeri==1
keep if cohort_retir65==1

gen agem_benefits=(month_benefits-birth_month)/12 


* Figure 1A
sum agem_benefits, det
global mn=round(r(mean),0.1) // -0.00000000000001
global md=round(r(p50), 0.1) // 
hist age_benefits , frac discrete ///
	scheme(s1mono) color(gs9) fcolor(gs10) ///
	xtitle("Retirement benefit start age") ///
	xline(60, lp(solid))  xline(${mn}, lp(dash)) xline(${md}, lp(shortdash)) ///
	text(.19 ${md} "Median=${md}", placement(east) size(small) color(gs5)) text(0.2 ${mn} "Mean=${mn}", placement(east) size(small) color(gs5))
graph export "`dir_output'/figure1a.png", replace 


* Figure 1B 
sum agem_benefits, det
global mn=round(r(mean),0.1) // -0.00000000000001
global md=round(r(p50), 0.1) // 
cdfplot agem_benefits ,  ///
	scheme(s1mono) /// opt1(lc(navy) ) /// color(gs9) fcolor(gs10) ///
	xtitle("Retirement benefit start age") ///
	xline(60, lp(solid))  xline(${mn}, lp(dash) ) xline(${md}, lp(shortdash)) /// lc(gs9)
	xscale(r(40 80) noextend) xlabel(40(5)80) yscale(r(0 1)) ylabel(0(.2)1) ///
	text(.9 ${md} "Median=${md}", placement(east) size(small) color(gs5)) text(0.95 ${mn} "Mean=${mn}", placement(east) size(small) color(gs5))
graph export "`dir_output'/figure1b.png", replace 

clear

exit
